using System.Collections.Generic;
using GameKit.Core;
using GameKit.Core.Context;

namespace GameKitUnity.Network
{
    public class SocketSystem : ISystem
    {
        private const string Tag = nameof(SocketSystem);
        private readonly Dictionary<string, ISocket> _sockets = new();

        public IContext Context { get; set; }

        public void OnInitialize()
        {
        }

        public void OnUpdate(float deltaTime)
        {
        }

        public void OnDestroy()
        {
        }

        public bool TryAddSocket(string name, ISocket socket)
        {
            if (_sockets.TryAdd(name, socket))
            {
                Log.Info(Tag, $"Add socket {name} success");
                return true;
            }

            Log.Error(Tag, $"Add socket {name} failed");
            return false;
        }

        public void Connect(string name, ISocket socket)
        {
            if (TryAddSocket(name, socket))
            {
                socket.Connect();
            }
        }
    }
}